package handlers

import (
	"github.com/gin-gonic/gin"
	"soft-manage/model"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("img")
	sign := c.PostForm("sign")
	if title == "" || content == "" || img == "" || sign == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	soft := model.Soft{
		Title:   title,
		Content: content,
		Img:     img,
		Sign:    sign,
	}
	err := model.DB.Create(&soft).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	var soft []model.Soft
	model.DB.Find(&soft)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": soft,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")

	var soft model.Soft
	model.DB.Where("id = ?", id).First(&soft)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": soft,
	})
	return

}
